'use strict';

var load = 1;
var page = 1;
$(function(){
    paged(page)

    // 界面加载
    $.ajax({
        type:"post",
        url:url+'/trade/trading.html?token='+token,
        async:true,
        data:{'token':token},
        dataType:'json',
        success:function(result){
            console.log(result)
            if(result.status === "0001"){
                var data = result.data;
                var x = data.trend.hTime;
                var y = data.trend.dList;
                var min = data.trend.min;

                kd(x,y,min);
                $("#price").html(data.price);
                
                // 购买数量
                var tradenum = data.tradenum;
                var tra_html = "";
                for(var i = 0;i < tradenum.length;i++){
                    var o = tradenum[i];
                    if(i === 0){
                        tra_html += `<div class="trantop_linkd"><a href="javascript:;" class="trantop_link on" data-id="${o.id}">${o.num}</a></div>`
                    }else{
                        tra_html += `<div class="trantop_linkd"><a href="javascript:;" class="trantop_link" data-id="${o.id}">${o.num}</a></div>`
                    }
                }
                $(".trantop_list").html(tra_html);
                if(tradenum.length === 6){
                    $(".trantop_linkd").css("width","33.33%");
                }
                // 合计价格
                var number = $(".trantop_link.on").html();
                $("#price2").html((Number(data.price) * Number(number)).toFixed(4));
            }else{
                isErr(result)
            }
        },
        error:function(){
            layer.msg("响应失败");
        }
    })

    // 申请购买
    $(".trantop_tlink").on("click",function(){
        $("#number").html($(".trantop_link.on").html());
        $("#dia_price1").html($("#price").html() + " USD");
        $("#dia_price2").html($("#price2").html() + " USD");
        dialog('#d1')
    });
    
})

// 确认弹窗
function sell(e,id){
    dialog("#d2")
    $("#hidden").val(id);
    var shen_number = $(e).parents("tr").find("td").eq(0).html();
    var shen_price1 = $(e).parents("tr").find("td").eq(1).html();
    var shen_price2 = $(e).parents("tr").find("td").eq(2).html();
    $("#dia_trat2").html("确认将出售 " + shen_number + " SDC余额");
    $("#shen_number").html(shen_number);
    $("#shen_price1").html(shen_price1 + " USD");
    $("#shen_price2").html(shen_price2 + " USD");
}

// 卖出列表
function paged(page){
    $.ajax({
        type:"post",
        url:url+'/trade/buylist.html?token='+token,
        async:true,
        data:{'token':token,"page":page},
        dataType:'json',
        success:function(result){
            // console.log(result)
            if(result.message == "交易大厅已关闭"){
                layer.msg(result.message);
            }

            if(result.status === "0001"){
                //卖出列表
                var list = result.data.list;
                var list_html = "";
                var thead_html = "";
                if(result.data.isuser === 1){
                    thead_html = `
                    <tr>
						<th>数量/个</th>
						<th>单价/USD</th>
						<th>总价/USD</th>
						<th>买家</th>
						<th>操作</th>
                    </tr>`
                    $(".tranbot_table thead").html(thead_html);
                }else{
                    thead_html = `
                    <tr>
						<th>数量/个</th>
						<th>单价/USD</th>
						<th>总价/USD</th>
						<th>操作</th>
                    </tr>`
                    $(".tranbot_table thead").html(thead_html);
                }
                    
                if(list != undefined){
                    if(list.length > 0){
                        for(var i = 0;i < list.length;i++){
                            var o = list[i];
                            list_html += `
                            <tr>
                                <td>${o.number}</td>
                                <td>${o.sysprice}</td>
                                <td>${o.samount}</td>`
                                if(result.data.isuser === 1){
                                    list_html += `<td style="width:1rem;display:block;line-height:.88rem; overflow: hidden;text-overflow:ellipsis;white-space: nowrap;">${o.in_username}</td>`
                                }
                                list_html += `<td><a href="javascript:;" class="tranbot_link" onclick="sell(this,${o.id})">卖给他</a></td>
                            </tr>`
                        }
                        if(list.length < 51){
                            load = 0;
                        }
                        $(".tranbot_table tbody").append(list_html);
                        return false;
                    }else{
                        load = 0;
                        return false;
                    }
                }else{
                    load = 0;
                    list_html += `
                        <tr><td colspan="4">暂无更多数据</td></tr>`
                    $(".tranbot_table tbody").append(list_html);
                }
                
            }else{
                isErr(result)
            }
        },
        error:function(){
            layer.msg("响应失败");
        }
    })
}

// 下拉加载更多
// $(window).scroll(function () {
//     if ($(document).scrollTop() >= $(document).height() - $(window).height() - 1) {
//         if (load == 1) {
//             page++;
//             paged(page);
//         }
//     }
// })

// k线图
function kd(data1,data2,min){
    var myChart2 = echarts.init(document.getElementById('main2'));
    // 指定图表的配置项和数据
    var option2 = {
        title: {
            text: 'SDC交易最新价格走势图',
            x:'left',
            y:'top',
            textStyle: {"fontSize":"12","color":"#fff"}
        },
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            // data: ['', '', '', '', '', '', ''],
            data: data1,
            axisLine:{
                lineStyle:{
                    color:'#fff'
                }
            }
        },
        yAxis: {
            type: 'value',
            min:min,
            axisLabel:{
                margin:2
            },
            axisLine:{
                lineStyle:{
                    color:'#fff'
                }
            }
        },
        series: [{
            // data: [820, 932, 901, 934, 1290, 1330, 1320],
            data: data2,
            type: 'line',
            symbol:'none', //去掉折线图中的节点
            smooth: true,  //true 为平滑曲线，false为直线
            itemStyle: {
                normal: {
                    areaStyle: {
                        type: 'default'
                    },
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                        offset: 0,
                        color: 'rgba(0,122,255,.6)'},
                    {
                        offset: 1,
                        color: 'rgba(0,122,255,.3)'
                    }]),
                    lineStyle:{
                        color:'#007AFF'  
                    } 
                }
            },
        }]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart2.setOption(option2);
}